package com.yibing.test.generic.generic_singleton_factory;

/**
 * This class is used to generate kinds of Unary Function Objects. 
 * @author Yibing
 *
 */
public class UnaryFunctionFactory {

	private UnaryFunctionFactory() {}
	
	/**
	 * This static member serves as a singleton of kinds of PrintOnce function objects that
	 * are obligated to be created with different parameter types
	 * To use this pattern, the "singleton" must be stateless and has nothing to do with 
	 * the concrete type.
	 */
	private static UnaryFunction<Object> PRINT_ONCE_FUNC = new UnaryFunction<Object>() {
		public Object apply(Object arg) {
			System.out.println(arg);
			return arg;
		}
	};
	
	@SuppressWarnings("unchecked")
	public static <T> UnaryFunction<T> getPrintOnceInst() {
		return (UnaryFunction<T>) PRINT_ONCE_FUNC;
	}
	
	private static UnaryFunction<Object> PRINT_TWICE_FUNC = new UnaryFunction<Object>() {
		public Object apply(Object arg) {
			System.out.println(arg);
			System.out.println(arg);
			return arg;
		}
	};
	
	@SuppressWarnings("unchecked")
	public static <T> UnaryFunction<T> getPrintTwiceInst() {
		return (UnaryFunction<T>) PRINT_TWICE_FUNC;
	}
	
}
